home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch13 / Axon.frm (.txt) next >
Visual Basic Form  |  1999-06-19  |  12KB  |  386 lines

  1. VERSION 5.00
  2. Begin VB.Form frmAxon 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Axon"
  6.    ClientHeight    =   5415
  7.    ClientLeft      =   1215
  8.    ClientTop       =   720
  9.    ClientWidth     =   6750
  10.    BeginProperty Font 
  11.       Name            =   "MS Sans Serif"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   700
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    KeyPreview      =   -1  'True
  21.    LinkTopic       =   "Form1"
  22.    PaletteMode     =   1  'UseZOrder
  23.    ScaleHeight     =   5415
  24.    ScaleWidth      =   6750
  25.    Begin VB.PictureBox Pict 
  26.       AutoRedraw      =   -1  'True
  27.       Height          =   2175
  28.       Index           =   5
  29.       Left            =   4560
  30.       ScaleHeight     =   -10
  31.       ScaleLeft       =   -5
  32.       ScaleMode       =   0  'User
  33.       ScaleTop        =   5
  34.       ScaleWidth      =   10
  35.       TabIndex        =   10
  36.       Top             =   2760
  37.       Width           =   2175
  38.    End
  39.    Begin VB.PictureBox Pict 
  40.       AutoRedraw      =   -1  'True
  41.       Height          =   2175
  42.       Index           =   4
  43.       Left            =   2280
  44.       ScaleHeight     =   -10
  45.       ScaleLeft       =   -5
  46.       ScaleMode       =   0  'User
  47.       ScaleTop        =   5
  48.       ScaleWidth      =   10
  49.       TabIndex        =   4
  50.       Top             =   2760
  51.       Width           =   2175
  52.    End
  53.    Begin VB.PictureBox Pict 
  54.       AutoRedraw      =   -1  'True
  55.       Height          =   2175
  56.       Index           =   3
  57.       Left            =   0
  58.       ScaleHeight     =   -10
  59.       ScaleLeft       =   -5
  60.       ScaleMode       =   0  'User
  61.       ScaleTop        =   5
  62.       ScaleWidth      =   10
  63.       TabIndex        =   3
  64.       Top             =   2760
  65.       Width           =   2175
  66.    End
  67.    Begin VB.PictureBox Pict 
  68.       AutoRedraw      =   -1  'True
  69.       Height          =   2175
  70.       Index           =   2
  71.       Left            =   4560
  72.       ScaleHeight     =   -10
  73.       ScaleLeft       =   -5
  74.       ScaleMode       =   0  'User
  75.       ScaleTop        =   5
  76.       ScaleWidth      =   10
  77.       TabIndex        =   2
  78.       Top             =   0
  79.       Width           =   2175
  80.    End
  81.    Begin VB.PictureBox Pict 
  82.       AutoRedraw      =   -1  'True
  83.       Height          =   2175
  84.       Index           =   1
  85.       Left            =   2280
  86.       ScaleHeight     =   -10
  87.       ScaleLeft       =   -5
  88.       ScaleMode       =   0  'User
  89.       ScaleTop        =   5
  90.       ScaleWidth      =   10
  91.       TabIndex        =   1
  92.       Top             =   0
  93.       Width           =   2175
  94.    End
  95.    Begin VB.PictureBox Pict 
  96.       AutoRedraw      =   -1  'True
  97.       Height          =   2175
  98.       Index           =   0
  99.       Left            =   0
  100.       ScaleHeight     =   -10
  101.       ScaleLeft       =   -5
  102.       ScaleMode       =   0  'User
  103.       ScaleTop        =   5
  104.       ScaleWidth      =   10
  105.       TabIndex        =   0
  106.       Top             =   0
  107.       Width           =   2175
  108.    End
  109.    Begin VB.Label Label1 
  110.       Alignment       =   2  'Center
  111.       Caption         =   "The final projection"
  112.       Height          =   255
  113.       Index           =   5
  114.       Left            =   4560
  115.       TabIndex        =   11
  116.       Top             =   5040
  117.       Width           =   2175
  118.       WordWrap        =   -1  'True
  119.    End
  120.    Begin VB.Label Label1 
  121.       Alignment       =   2  'Center
  122.       Caption         =   "Project"
  123.       Height          =   255
  124.       Index           =   4
  125.       Left            =   2280
  126.       TabIndex        =   9
  127.       Top             =   5040
  128.       Width           =   2175
  129.       WordWrap        =   -1  'True
  130.    End
  131.    Begin VB.Label Label1 
  132.       Alignment       =   2  'Center
  133.       Caption         =   "Rotate into Y axis"
  134.       Height          =   255
  135.       Index           =   3
  136.       Left            =   0
  137.       TabIndex        =   8
  138.       Top             =   5040
  139.       Width           =   2175
  140.       WordWrap        =   -1  'True
  141.    End
  142.    Begin VB.Label Label1 
  143.       Alignment       =   2  'Center
  144.       Caption         =   "Rotate into Y-Z plane"
  145.       Height          =   255
  146.       Index           =   2
  147.       Left            =   4560
  148.       TabIndex        =   7
  149.       Top             =   2280
  150.       Width           =   2175
  151.       WordWrap        =   -1  'True
  152.    End
  153.    Begin VB.Label Label1 
  154.       Alignment       =   2  'Center
  155.       Caption         =   "Translate to origin"
  156.       Height          =   255
  157.       Index           =   1
  158.       Left            =   2280
  159.       TabIndex        =   6
  160.       Top             =   2280
  161.       Width           =   2175
  162.       WordWrap        =   -1  'True
  163.    End
  164.    Begin VB.Label Label1 
  165.       Alignment       =   2  'Center
  166.       Caption         =   "Original picture"
  167.       Height          =   255
  168.       Index           =   0
  169.       Left            =   0
  170.       TabIndex        =   5
  171.       Top             =   2280
  172.       Width           =   2175
  173.       WordWrap        =   -1  'True
  174.    End
  175. Attribute VB_Name = "frmAxon"
  176. Attribute VB_GlobalNameSpace = False
  177. Attribute VB_Creatable = False
  178. Attribute VB_PredeclaredId = True
  179. Attribute VB_Exposed = False
  180. Option Explicit
  181. ' Location of viewing eye.
  182. Private EyeR As Single
  183. Private EyeTheta As Single
  184. Private EyePhi As Single
  185. ' Location of focus point.
  186. Private Const FocusX = 0#
  187. Private Const FocusY = 0#
  188. Private Const FocusZ = 0#
  189. Private Projector(1 To 4, 1 To 4) As Single
  190. ' The transformation matrices.
  191. Private M(0 To 4) As Transformation
  192. ' First segment not in the axes.
  193. Private FirstSegment As Integer
  194. ' Create the matrices used when performing an
  195. ' axonometric orthographic projection with focus
  196. ' at (f1, f2, f3) and projection direction
  197. ' <n1, n2, n3>.
  198. Private Sub CreateMatrices(ByVal f1 As Single, ByVal f2 As Single, ByVal f3 As Single, ByVal n1 As Single, ByVal n2 As Single, ByVal n3 As Single)
  199. Dim trans(1 To 4, 1 To 4) As Single
  200. Dim Rot1(1 To 4, 1 To 4) As Single
  201. Dim Rot2(1 To 4, 1 To 4) As Single
  202. Dim Proj(1 To 4, 1 To 4) As Single
  203. Dim D As Single
  204. Dim L As Single
  205.     ' Translate the focus point to the origin.
  206.     m3Translate trans, -f1, -f2, -f3
  207.     ' Rotate around Z-axis until the projection
  208.     ' direction is in the Y-Z plane.
  209.     m3Identity Rot1
  210.     D = Sqr(n1 * n1 + n2 * n2)
  211.     Rot1(1, 1) = n2 / D
  212.     Rot1(1, 2) = n1 / D
  213.     Rot1(2, 1) = -Rot1(1, 2)
  214.     Rot1(2, 2) = Rot1(1, 1)
  215.     ' Rotate around the X-axis until the normal
  216.     ' lies along the Y axis.
  217.     m3Identity Rot2
  218.     L = Sqr(n1 * n1 + n2 * n2 + n3 * n3)
  219.     Rot2(2, 2) = D / L
  220.     Rot2(2, 3) = -n3 / L
  221.     Rot2(3, 2) = -Rot2(2, 3)
  222.     Rot2(3, 3) = Rot2(2, 2)
  223.     ' Project into the X-Z plane.
  224.     m3Identity Proj
  225.     Proj(2, 2) = 0
  226.     ' Put the matrices in the M array.
  227.     m3Identity M(0).M
  228.     m3MatCopy M(1).M, trans
  229.     m3MatCopy M(2).M, Rot1
  230.     m3MatCopy M(3).M, Rot2
  231.     m3MatCopy M(4).M, Proj
  232. End Sub
  233. ' Let the user change the location of the eye.
  234. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  235. Const Dtheta = PI / 20
  236.     Select Case KeyCode
  237.         Case vbKeyLeft
  238.             EyeTheta = EyeTheta - Dtheta
  239.             
  240.         Case vbKeyRight
  241.             EyeTheta = EyeTheta + Dtheta
  242.         
  243.         Case vbKeyUp
  244.             EyePhi = EyePhi - Dtheta
  245.         
  246.         Case vbKeyDown
  247.             EyePhi = EyePhi + Dtheta
  248.         
  249.         Case Else
  250.             Exit Sub
  251.     End Select
  252.     ' Make a new projection matrix.
  253.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  254.     ' Redraw the pictures.
  255.     DrawAllData
  256. End Sub
  257. ' Rotate the points in the cube and draw the cube.
  258. Private Sub DrawTheData(ByVal pic As Object, ByVal project As Boolean)
  259. Dim i As Integer
  260. Dim x1 As Single
  261. Dim y1 As Single
  262. Dim x2 As Single
  263. Dim y2 As Single
  264. Dim oldwidth As Integer
  265.     ' If we should project, do so.
  266.     If project Then TransformData Projector, 1, NumSegments
  267.     ' Draw the points.
  268.     pic.Cls
  269.     oldwidth = pic.DrawWidth
  270.     For i = 1 To NumSegments
  271.         x1 = Segments(i).fr_tr(1)
  272.         y1 = Segments(i).fr_tr(2)
  273.         x2 = Segments(i).to_tr(1)
  274.         y2 = Segments(i).to_tr(2)
  275.         
  276.         ' Draw the plane's normal in bold.
  277.         If i = 4 Then pic.DrawWidth = 3
  278.         pic.Line (x1, y1)-(x2, y2)
  279.         If i = 4 Then pic.DrawWidth = oldwidth
  280.     Next i
  281.     pic.Refresh
  282. End Sub
  283. Private Sub Form_Load()
  284.     ' Initialize the eye position.
  285.     EyeR = 3
  286.     EyeTheta = PI * 0.4
  287.     EyePhi = PI * 0.2
  288.     ' Create the initial viewing transformation.
  289.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  290.     ' Create the projection matrices.
  291.     CreateMatrices 2, 2, 2, 2, 1, 1
  292.     ' Create, project, and draw the data.
  293.     DrawAllData
  294. End Sub
  295. ' Draw all the pictures.
  296. Private Sub DrawAllData()
  297. Dim i As Integer
  298. Dim P(1 To 4, 1 To 4) As Single
  299.     ' Start with fresh data.
  300.     CreateData
  301.     For i = 0 To 4
  302.         ' Apply the next transformation.
  303.         TransformData M(i).M, FirstSegment, NumSegments
  304.         SetPoints FirstSegment, NumSegments
  305.         
  306.         ' Display the data.
  307.         DrawTheData Pict(i), True
  308.     Next i
  309.     ' Create the final, transformed picture.
  310.     m3OrthoTop P
  311.     TransformData P, 1, NumSegments
  312.     DrawTheData Pict(5), False
  313. End Sub
  314. ' Create the cube data.
  315. Private Sub CreateData()
  316. Dim L As Single
  317. Dim v1x As Single
  318. Dim v1y As Single
  319. Dim v1z As Single
  320. Dim v2x As Single
  321. Dim v2y As Single
  322. Dim v2z As Single
  323. Dim p1x As Single
  324. Dim p1y As Single
  325. Dim p1z As Single
  326. Dim p2x As Single
  327. Dim p2y As Single
  328. Dim p2z As Single
  329. Dim p3x As Single
  330. Dim p3y As Single
  331. Dim p3z As Single
  332. Dim p4x As Single
  333. Dim p4y As Single
  334. Dim p4z As Single
  335.     ' Start with no data.
  336.     NumSegments = 0
  337.     ' Create the axes.
  338.     MakeSegment 0, 0, 0, 5, 0, 0    ' X axis.
  339.     MakeSegment 0, 0, 0, 0, 5, 0    ' Y axis.
  340.     MakeSegment 0, 0, 0, 0, 0, 5    ' Z axis.
  341.     FirstSegment = NumSegments + 1
  342.     ' Make a projection direction vector.
  343.     MakeSegment 2, 2, 2, 4, 3, 3
  344.     ' Create the edges of the projection plane.
  345.     m3Cross v1x, v1y, v1z, 2, 1, 1, 0, 1, 0
  346.     L = Sqr(v1x * v1x + v1y * v1y + v1z * v1z)
  347.     v1x = 3 * v1x / L
  348.     v1y = 3 * v1y / L
  349.     v1z = 3 * v1z / L
  350.     m3Cross v2x, v2y, v2z, 2, 1, 1, v1x, v1y, v1z
  351.     L = Sqr(v2x * v2x + v2y * v2y + v2z * v2z)
  352.     v2x = 3 * v2x / L
  353.     v2y = 3 * v2y / L
  354.     v2z = 3 * v2z / L
  355.     p1x = 2 + v1x + v2x
  356.     p1y = 2 + v1y + v2y
  357.     p1z = 2 + v1z + v2z
  358.     p2x = 2 - v1x + v2x
  359.     p2y = 2 - v1y + v2y
  360.     p2z = 2 - v1z + v2z
  361.     p3x = 2 - v1x - v2x
  362.     p3y = 2 - v1y - v2y
  363.     p3z = 2 - v1z - v2z
  364.     p4x = 2 + v1x - v2x
  365.     p4y = 2 + v1y - v2y
  366.     p4z = 2 + v1z - v2z
  367.     MakeSegment p1x, p1y, p1z, p2x, p2y, p2z
  368.     MakeSegment p2x, p2y, p2z, p3x, p3y, p3z
  369.     MakeSegment p3x, p3y, p3z, p4x, p4y, p4z
  370.     MakeSegment p4x, p4y, p4z, p1x, p1y, p1z
  371.     ' Create a cube to project.
  372.     MakeSegment 1, 1, 1, 1, 3, 1
  373.     MakeSegment 1, 3, 1, 3, 3, 1
  374.     MakeSegment 3, 3, 1, 3, 1, 1
  375.     MakeSegment 3, 1, 1, 1, 1, 1
  376.     MakeSegment 1, 1, 3, 1, 3, 3
  377.     MakeSegment 1, 3, 3, 3, 3, 3
  378.     MakeSegment 3, 3, 3, 3, 1, 3
  379.     MakeSegment 3, 1, 3, 1, 1, 3
  380.     MakeSegment 1, 1, 1, 1, 1, 3
  381.     MakeSegment 1, 3, 1, 1, 3, 3
  382.     MakeSegment 3, 3, 1, 3, 3, 3
  383.     MakeSegment 3, 1, 1, 3, 1, 3
  384.     NumSegments = NumSegments
  385. End Sub
  386.